<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="大类" prop="bigCategory">
        <el-input
          v-model="queryParams.bigCategory"
          placeholder="请输入大类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类目" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入类目"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input
          v-model="queryParams.unitPrice"
          placeholder="请输入单价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="linkMan">
        <el-input
          v-model="queryParams.linkMan"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="审核状态" prop="state">
       <el-select v-model="queryParams.state" placeholder="请选择审核状态">
          <el-option
            v-for="item in authState"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['internet:intBrand:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['internet:intBrand:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['internet:intBrand:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['internet:intBrand:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="intBrandList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" show-overflow-tooltip prop="title" />
      <el-table-column label="大类" align="center" prop="bigCategory" />
      <el-table-column label="类目" align="center" prop="category" />
      <el-table-column label="材质" align="center" prop="texture" />
      <el-table-column label="产地" align="center" prop="producingArea" />
      <el-table-column
        label="可交易总量(个)"
        align="center"
        prop="tradableTotal"
      />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="联系人" align="center" prop="linkMan" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column
        label="发布失效日期"
        align="center"
        prop="expirationDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品图" align="center" prop="goodsPic" >
          <template slot-scope="scope">
          <img
            :src="scope.row.goodsPicArray"
            alt
            style="width: 80px; height: 80px; object-fit: cover"
          /> </template
        ><strong />
      </el-table-column>
      <el-table-column label="发布商" align="center" prop="expirationName" />
      <el-table-column label="审核状态" align="center" prop="stateNew" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200" fixed="right"
      >
        <template slot-scope="scope" >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['internet:intBrand:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
              v-if="scope.row.stateNew === '审核中'"
            icon="el-icon-delete"
            @click="handle(scope.row)"
            v-hasPermi="['internet:intBrand:remove']"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改品牌中心对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input :disabled="delFlag" v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="大类" prop="bigCategory">
          <el-input :disabled="delFlag" v-model="form.bigCategory" placeholder="请输入大类" />
        </el-form-item>
        <el-form-item label="类目" prop="category">
          <el-input :disabled="delFlag" v-model="form.category" placeholder="请输入类目" />
        </el-form-item>
        <el-form-item label="材质" prop="texture">
          <el-input :disabled="delFlag" v-model="form.texture" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="产地" prop="producingArea">
          <el-input :disabled="delFlag" v-model="form.producingArea" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="可交易总量(个)" prop="tradableTotal">
          <el-input
          :disabled="delFlag"
            v-model="form.tradableTotal"
            placeholder="请输入可交易总量(个)"
          />
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input :disabled="delFlag" v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="联系人" prop="linkMan">
          <el-input :disabled="delFlag" v-model="form.linkMan" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input :disabled="delFlag" v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="发布失效日期" prop="expirationDate">
          <el-date-picker
          :disabled="delFlag"
            clearable
            size="small"
            v-model="form.expirationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发布失效日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品图" prop="goodsPicArray">
    <div class="block" v-for="item in form.goodsPicArray" :key="item">
            <el-image
           disabled
              style="width: 100px; height: 100px"
              :src="item"
              fit="fill"
            ></el-image>
          </div>
        </el-form-item>
        </el-form-item>
        <el-form-item label="发布商" prop="expirationName">
          <el-input
          :disabled="delFlag"
            v-model="form.expirationName"
            placeholder="请输入发布商"
          />
        </el-form-item>
        <el-form-item label="审核状态" prop="state">
          <el-select  :disabled="delFlag" v-model="form.state" placeholder="请选择审核状态">
          <el-option
            v-for="item in authState"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
           :disabled="delFlag"
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item> 
        <el-form-item label="是否通过审核" v-if='isHandle'>
          <el-radio-group v-model="examine" >
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="未通过审核原因" prop="reason" v-if='isHandle&&examine==="1"'>
          <el-input v-model="form.reason" placeholder="请输入未通过审核原因" />
        </el-form-item>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if='isHandle' @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listIntBrand,
  getIntBrand,
  delIntBrand,
  addIntBrand,
  updateIntBrand,
} from "@/api/indusInternet/brand";
import { listData } from "@/api/system/dict/data";

export default {
  name: "IntBrand",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 品牌中心表格数据
      intBrandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        bigCategory: null,
        category: null,
        texture: null,
        producingArea: null,
        tradableTotal: null,
        unitPrice: null,
        linkMan: null,
        phone: null,
        expirationDate: null,
        goodsPicId: null,
        expirationUserId: null,
        state: null,
        reason: null,
      },
      examine: "0",
      delFlag: false,
      isHandle: false, //审核
      authState: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
          reason: [
          {
            required: true,
            message: "请输入未通过原因",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询互联网用户列表 */
    getdictDataList() {
      listData({ dictType: "publish_state" }).then((response) => {
        this.authState = response.rows;
      });
    },
    /** 查询品牌中心列表 */
  async getList() {
      this.loading = true;
      await this.getdictDataList()
      listIntBrand(this.queryParams).then((response) => {
        response.rows.forEach((item) => {
           if (item.goodsPicArray) {
            item.goodsPicArray = item.goodsPicArray[0]
              ? item.goodsPicArray[0]
              : "";
          } else {
            item.goodsPicArray = "";
          }
          this.authState.forEach((itemType) => {
            if (itemType.dictValue === item.state) {
              item.stateNew = itemType.dictLabel;
            }
          });
        });
        this.intBrandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        bigCategory: null,
        category: null,
        texture: null,
        producingArea: null,
        tradableTotal: null,
        unitPrice: null,
        linkMan: null,
        phone: null,
        expirationDate: null,
        goodsPicId: null,
        expirationUserId: null,
        state: null,
        reason: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加品牌中心";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
       this.delFlag = true;
      this.isHandle = false;
      getIntBrand(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "品牌中心查看";
      });
    },
     handle(row) {
      this.reset();
      const id = row.id || this.ids;
      this.delFlag = true;
      this.isHandle = true;
    getIntBrand(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "品牌中心审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        
        if (valid) {
          if (this.examine === "0") {
            this.form.state = "2";
          } else {
            this.form.state = "5";
          }
            updateIntBrand(this.form).then((response) => {
              this.msgSuccess("发送成功");
              this.open = false;
              this.getList();
            });
          } 
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除品牌中心编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delIntBrand(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "internet/intBrand/export",
        {
          ...this.queryParams,
        },
        `internet_intBrand.xlsx`
      );
    },
  },
};
</script>